package com.zhuliwei.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhuliwei.domain.ResponseResult;
import com.zhuliwei.domain.entity.Notice;
import com.zhuliwei.service.NoticeService;
import com.zhuliwei.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 朱力维
 * @time 2024/11/30 17:35
 **/
@RestController
@RequestMapping(("/notice"))
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @GetMapping
    public ResponseResult getNoticesByUserId() {
        return ResponseResult.okResult(noticeService.getNoticesByUserId());
    }

    @DeleteMapping("/{id}")
    public ResponseResult del(@PathVariable String id) {
        noticeService.removeById(id);
        return ResponseResult.okResult();
    }

    @PutMapping("/read/{id}")
    public ResponseResult readNotice(@PathVariable String id) {
        Notice notice = noticeService.getById(id);
        // 设为已读
        notice.setIsRead("1");
        noticeService.updateById(notice);
        return ResponseResult.okResult();
    }

    // 未读数量
    @GetMapping("/nonReadCount")
    public ResponseResult nonReadCount() {
        String myId = SecurityUtils.getUserId();
        LambdaQueryWrapper<Notice> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Notice::getUserId, myId);
        lqw.eq(Notice::getIsRead, "0");
        long count = noticeService.count(lqw);
        return ResponseResult.okResult(count);
    }
}
